from quart import Blueprint, request, jsonify
from src.service.works_service import WorksService
from loguru import logger

# 创建蓝图
main_list_route = Blueprint('main_list_route', __name__)

@main_list_route.route('/api/works/list', methods=['GET'])
async def get_works_list():
    """
    获取作品列表API接口
    支持分页查询
    """
    try:
        # 获取查询参数
        page = request.args.get('page', 1, type=int)
        page_size = request.args.get('page_size', 10, type=int)
        
        # 参数验证
        if page < 1:
            return jsonify({
                'code': 400,
                'msg': 'page参数必须大于0',
                'data': None
            }), 400
        
        if page_size < 1 or page_size > 100:
            return jsonify({
                'code': 400,
                'msg': 'page_size参数必须在1-100之间',
                'data': None
            }), 400
        
        # 调用服务获取数据
        works_service = WorksService()
        result = await works_service.get_works_list(page=page, page_size=page_size)
        
        logger.info(f"获取作品列表成功: page={page}, page_size={page_size}, total={result['total']}")
        
        return jsonify({
            'code': 0,
            'msg': 'Success',
            'data': result
        })
        
    except Exception as e:
        logger.error(f"获取作品列表失败: {str(e)}")
        return jsonify({
            'code': 500,
            'msg': f'Internal server error: {str(e)}',
            'data': None
        }), 500